<?php

/**
 * Created by PhpStorm.
 * User: d7
 * Date: 2016/1/27
 * Time: 11:04
 */
class Place extends MY_Api_Controller
{
    protected $_page_size;
    function __construct(){
        parent::__construct();
        $this->_page_size = $this->config->item('page_size','default');
    }


    function gets(){
        $recommend = $this->input->get('recommend');
        if(empty($page) and !empty($recommend) and $recommend=='index'){
            $this->config->load('data/recommend');
            $places = $this->config->item('index')['recommend_place'];
        }else{
            $this->load->model('place/Place_model');
            $page = $this->input->get('page');
            $page = $page>0 ? $page : 1;
            $field  = $this->input->get('field');
            $field  = empty($field)?'id':$field;
            $sort   = $this->input->get('sort')=='asc'?'asc':'desc';
            //$status = $this->input->get('status');
            $condition = array(
                'is_del'    =>  0,
            );
            //if($status!=='' or $status!==null)
            //    $condition['status'] = $status;
            $options = array(
                'limit' =>array(
                    $this->_page_size,
                    ($page-1)*$this->_page_size
                ),
                'order'=>
                    array($field.' '.$sort)
            );
            $places = $this->Place_model->fetch_array($condition,'name,cover,intro,score',$options);
        }

        if(!$places){
            $this->set_response(60605);
        }

        $this->load->helper('image');
        foreach($places as &$value){
            $value['cover'] = get_small_image($value['cover'],'_middle');
            $value = $this->machining($value,array(
                'content_key'   =>  'content',
                'image_key'     =>  'cover'
            ));
            unset($value);
        }

        $this->set_response(0,$places);
    }

    function get(){
        $id = $this->input->get('id');
        if($id<0){
            $this->set_response(60606);
        }
        $place = $this->Place_model->getOne(array('id'=>$id));
        if(!$place)
            $this->set_response(60605);
        $this->set_response(0,$place);
    }

    function get_comments(){
        $this->json_response(0);
        //todo:  next
    }
}